﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows.Input;
using OpenLightGroup.Common;
using OpenLightGroup.Common.MVVM;

namespace Examples.ViewModels
{
    public class ExampleViewModel
        : BaseViewModel
    {



        public ExampleViewModel()
        {
     


            this.DeleteCommand = new DelegateCommand(Delete, CanDelete);

            this.DateCreated = (DateTime.Now - new TimeSpan(1, 0, 0)).ToString();

            this.SelectedValue = 4;

        }

        private SelectionCollection<Item> _selectableItems = null;

        public SelectionCollection<Item> SelectableItems
        {
            get
            {

                if (_selectableItems == null)
                {
                    _selectableItems = new SelectionCollection<Item>(this.Items);
                }
                return _selectableItems;

            }
        }

        private int _selectedValue;

        public int SelectedValue
        {
            get { return _selectedValue; }
            set
            {
                if (_selectedValue != value)
                {
                    _selectedValue = value;
                    this.RaisePropertyChanged("SelectedValue");
                }
            }
        }

        private ICommand _clearSelectedValueCommand;

        public ICommand ClearSelectedValueCommand
        {
            get
            {
                if (this._clearSelectedValueCommand == null)
                {
                    this._clearSelectedValueCommand = new DelegateCommand(ClearSelectedValue);
                }
                return this._clearSelectedValueCommand;
            }
        }

        private void ClearSelectedValue(object arg)
        {
            this.SelectedValue = -1;
        }




        private string _dateCreated;

        public string DateCreated
        {
            get { return _dateCreated; }
            set
            {
                _dateCreated = value;

                this.RaisePropertyChanged("DateCreated");
                this.RaisePropertyChanged("CurrentTime");

            }
        }



        public DateTime CurrentTime
        {
            get
            {
                return DateTime.Now;
            }
        }




        private ICommand _addCommand;

        public ICommand AddCommand
        {
            get
            {
                if (this._addCommand == null)
                {
                    this._addCommand = new DelegateCommand(Add);
                }
                return this._addCommand;
            }
        }

        private void Add(object arg)
        {
            this.SelectableItems.Add(true, arg as Item);

            //var isSelected = (DateTime.Now.Ticks % 2) == 0;

            //this.SelectableItems.Add(isSelected, new Item() { Name = DateTime.Now.ToShortTimeString() });

        }

        private ICommand _selectAllCommand;

        public ICommand SelectAllCommand
        {
            get
            {
                if (this._selectAllCommand == null)
                {
                    this._selectAllCommand = new DelegateCommand(SelectAll);
                }
                return this._selectAllCommand;
            }
        }

        private void SelectAll(object arg)
        {
            this.SelectableItems.SelectAll();
        }

        private ICommand _unselectAllCommand;

        public ICommand UnselectAllCommand
        {
            get
            {
                if (this._unselectAllCommand == null)
                {
                    this._unselectAllCommand = new DelegateCommand(UnselectAll);
                }
                return this._unselectAllCommand;
            }
        }

        private void UnselectAll(object arg)
        {
            this.SelectableItems.UnselectAll();
        }




        public ICommand DeleteCommand { get; set; }


        private void Delete(object obj)
        {
            var selectedItems = this.SelectableItems.SelectedItems.ToArray();

            foreach (var item in selectedItems)
            {
                this.SelectableItems.Remove(item);
            }


        }
        private bool CanDelete(object arg)
        {
            return this.SelectableItems.SelectedItems.Count() > 0;
        }

    }
}
